package cn.gxa.edu_adm.servlet;

import cn.gxa.edu_adm.bean.Student;
import cn.gxa.edu_adm.commn.model.ResultData;
import cn.gxa.edu_adm.service.StudentService;
import cn.gxa.edu_adm.service.impl.StudentServiceImpl;
import cn.gxa.edu_adm.utils.StringUtils;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

@WebServlet(urlPatterns = "/studentAdd")
public class StudentAddServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //前端请求参数
        String  stuName=req.getParameter("stuName");
        Integer  stuSex=StringUtils.formatInteger(req.getParameter("stuSex"));
        Date stuBirthday=StringUtils.formatDate(req.getParameter("stuBirthday"));
        String  stuPhone=req.getParameter("stuPhone");
        String  stuIdCard=req.getParameter("stuIdCard");
        Integer  claId=StringUtils.formatInteger(req.getParameter("claId"));
        Integer  stuState=StringUtils.formatInteger(req.getParameter("stuState"));
        Student   student=new Student();
        student.setClaId(claId);
        student.setStuBirthday(stuBirthday);
        student.setStuIdCard(stuIdCard);
        student.setStuName(stuName);
        student.setStuPhone(stuPhone);
        student.setStuSex(stuSex);
        student.setStuState(stuState);

        //调用service获取结果集
        ResultData   resultData=new StudentServiceImpl().insert(student);
        //响应给前端
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSONString(resultData));
        writer.close();


    }
}
